package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.atguigu.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimall.product.service.ProductAttrValueService;
import com.atguigu.gulimall.product.vo.AttrRespVo;
import com.atguigu.gulimall.product.vo.AttrVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 商品属性
 *
 * @author lxx
 * @email 1914264274@qq.com
 * @date 2022-03-08 11:28:16
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    //更改spu规格属性
///product/attr/update/{spuId}
    @Transactional
    @RequestMapping("/update/{spuId}")
    public R updateSpuInfo(@PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> productAttrValueEntityList){
        //先删除后增加
        QueryWrapper<ProductAttrValueEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("spu_id",spuId);
        productAttrValueService.remove(wrapper);

        //增加
        List<ProductAttrValueEntity> collect = productAttrValueEntityList.stream().map(item -> {
            item.setSpuId(spuId);
            return item;
        }).collect(Collectors.toList());
        productAttrValueService.saveBatch(collect);
        return R.ok();
    }

    //获取spu规格
//    /product/attr/base/listforspu/{spuId}
    @RequestMapping("/base/listforspu/{spuId}")
    public R listForSpu(@PathVariable("spuId") Long spuId) {
        QueryWrapper<ProductAttrValueEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("spu_id", spuId);
        List<ProductAttrValueEntity> productAttrValueEntities = productAttrValueService.list(wrapper);
        return R.ok().put("data", productAttrValueEntities);
    }

    /**
     * 查询当前分类绑定的分组(如果是基本属性的就去查询分组信息，销售属性就没有分组信息)
     *
     * @param params
     * @param catId  catId 通过路径变量传递所属分类的第三级ID
     * @return
     */
//    @RequestMapping("/base/list/{catId}")
    @RequestMapping("/{type}/list/{catId}")
    public R baselist(@RequestParam Map<String, Object> params,
                      @PathVariable("catId") long catId,
                      @PathVariable("type") String type) {


//        PageUtils page = attrService.queryPage(params);
        PageUtils pageUtils = attrService.queryCatGroup(params, catId, type);


        return R.ok().put("page", pageUtils);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
        //查询详情（顺带查询出当属性的详细信息）
//		AttrEntity attr = attrService.getById(attrId);

        AttrRespVo attrRespVo = attrService.getAttrDetail(attrId);


        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存(基本属性的保存需要在中间表中保存属性分组ID，销售属性部需要往中间表中保存数据)
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo) {
//		attrService.save(attr);

        attrService.saveAttrVo(attrVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr) {


//		attrService.updateById(attr);
        attrService.updateAttrInfo(attr);


        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
